package com.changyi.device.printer;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.changyi.device.TestXin;
import com.google.zxing.BarcodeFormat;
import com.sunmi.peripheral.printer.SunmiPrinterService;
import com.zqprintersdk.PrinterConst;
import com.zqprintersdk.ZQPrinterSDK;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xin on 2021/3/29
 * desc:
 */
public class ZQM2Printer implements PrinterInterface {

    private Context mContext;
    private static ZQM2Printer zqm2Printer;
    public ZQPrinterSDK zqPrn = null;

    private ZQM2Printer(Context context) {
        mContext = context;
    }

    public static ZQM2Printer getInstance(Context context) {
        if (zqm2Printer == null) {
            synchronized (LandiA8Printer.class){
                if (zqm2Printer == null) {
                    zqm2Printer = new ZQM2Printer(context);
                }
            }
        }
        return zqm2Printer;
    }


    @Override
    public PrinterInterface initDevice() {
        zqPrn = new ZQPrinterSDK();
        new Thread(new Runnable() {
            @Override
            public void run() {
                //USB0
                int nRet = zqPrn.Prn_Connect("ttyMT2:9600", mContext);
                if (nRet != PrinterConst.ErrorCode.SUCCESS)
                {
                    return;
                }
            }
        }).start();
        int nRet = zqPrn.Prn_SetAlignment(PrinterConst.Alignment.LEFT);
        nRet = zqPrn.Prn_SetFontSize(PrinterConst.WidthSize.SIZE0 | PrinterConst.HeightSize.SIZE0);


        if(nRet == 0){
            TestXin.writeLog("中器初始化成功!");
        }else{
            TestXin.writeLog("初始化失败!");
        }
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printTitle(String title) {
        TestXin.writeLog("标题!");
        int nRet = zqPrn.Prn_SetAlignment(PrinterConst.Alignment.LEFT);
        nRet = zqPrn.Prn_SetFontSize(PrinterConst.WidthSize.SIZE0 | PrinterConst.HeightSize.SIZE0);
        zqPrn.Prn_PrintEscText(title + "\r\n");
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printText(String text) {
        int nRet = zqPrn.Prn_SetAlignment(PrinterConst.Alignment.LEFT);
        nRet = zqPrn.Prn_SetFontSize(PrinterConst.WidthSize.SIZE0 | PrinterConst.HeightSize.SIZE0);
        zqPrn.Prn_PrintEscText(text + "\r\n");
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printList(List<String> mList) {
        TestXin.writeLog("内容!");
        int nRet = zqPrn.Prn_SetAlignment(PrinterConst.Alignment.LEFT);
        nRet = zqPrn.Prn_SetFontSize(PrinterConst.WidthSize.SIZE0 | PrinterConst.HeightSize.SIZE0);
        List<String> codes=new ArrayList<>();
        for (String content : mList) {
            if(content.contains("|")){
                String[] aa = content.split("\\|");
                for (int i = 0 ; i <aa.length ; i++ ) {
                    zqPrn.Prn_PrintEscText(aa[i] + "\r\n");
                }
            }else  if (content.contains("@@@")){
                codes.add(content);

            } else  if (content.contains("&&&")){
                codes.add(content);
            } else if(content.equals("  ")){
                codes.add(content);
            }
            else{
                zqPrn.Prn_PrintEscText(content.trim() + "\r\n");
            }

        }
        zqPrn.Prn_LineFeed(3);

//        if(codes!=null&&codes.size()>0) {
//            printBitmaps(codes);
//        }
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printCode(String code) {
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printQrCode(String qRCode) {
        return zqm2Printer;
    }

    @Override
    public PrinterInterface feedLine(int feelLine) {
        zqPrn.Prn_LineFeed(feelLine);
        return zqm2Printer;
    }

    public PrinterInterface printBitmaps(List<String> codes) {
        initDevice();
        for (String content : codes) {
            if (content.contains("@@@")) {
                String temp = content.substring(3, content.length());
//                sdk.setPrnBitmap(ZXingUtils.encodeAsBitmap(temp, BarcodeFormat.QR_CODE, 200, 200), 1);

            } else if (content.contains("&&&")) {
                String temp = content.substring(3, content.length());
//                sdk.setPrnBitmap(ZXingUtils.creatBarcode(mContext, temp, 300, 60, false), 0);
            } else{
//                sdk.setPrnText(content.trim()+"\n", 1, 0);
            }
        }
//        sdk.feedPaper(10,0);
//        sdk.startPrint();
        return zqm2Printer;
    }


    private String getErrorDescription(int code) {
        switch (code) {
            case 0:
                return "打印成功!";
            case -52:
                return "打印机过热!";
            case -51:
                return "打印机缺纸!";
        }
        return "打印其他错误!";
    }
}
